/**
 * 将树形数组变为一维数组
 *  @param treeData 树形数据
 *  @param childrenKey 子节点key
 *  @returns 变换后数组
 */

export function flattenTreeData(treeData: any[], childrenKey = 'children') {
    const result: any[] = [];
    function flatten(nodes: any[], level = 0, parentPath = ''){
        nodes.forEach(node => {
            const newNode = {...node}
            //
            newNode._level = level
            newNode._parentPath = parentPath
            result.push(newNode)
            //处理子节点
            if(node[childrenKey] && node[childrenKey].length > 0){
                flatten(node[childrenKey], level+1, `${parentPath}/${node.id}`);
            }
            delete newNode[childrenKey]
        })
    }
    flatten(treeData)
    return result
}